package com.example.educationcommunity.controller;

import com.example.educationcommunity.dto.PagingDTO;
import com.example.educationcommunity.mapper.CommentMapper;
import com.example.educationcommunity.mapper.QuestionMapper;
import com.example.educationcommunity.mapper.UserMapper;
import com.example.educationcommunity.model.CommentExample;
import com.example.educationcommunity.model.QuestionExample;
import com.example.educationcommunity.model.User;
import com.example.educationcommunity.model.UserExample;
import com.example.educationcommunity.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class profileController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private CommentMapper commentMapper;

    @RequestMapping("/profile/{id}")
    private String profile(Model model,
                           @RequestParam(name="page",defaultValue = "1") Integer page,
                           @RequestParam(name = "size",defaultValue = "5") Integer size,
                           @PathVariable(name = "id") Integer id){
        PagingDTO pagination= questionService.list(page,size);
        model.addAttribute("pagination",pagination);
        User user = userMapper.selectByPrimaryKey(id);
        QuestionExample example = new QuestionExample();
        example.createCriteria()
                .andCreatorEqualTo(id);
        Integer questionNumber = (int) questionMapper.countByExample(example);
        CommentExample example1 = new CommentExample();
        example1.createCriteria()
                .andCommentatorEqualTo(id);
        Integer commentNumber = (int) commentMapper.countByExample(example1);
        model.addAttribute("questionNumber",questionNumber);
        model.addAttribute("commentNumber",commentNumber);
        model.addAttribute("user",user);
        model.addAttribute("id",id.toString());
        return "profile";
    }
}
